# -*- coding: utf-8 -*-
"""
@Time ： 2022/12/29 16:12
@Auth ： jiraWu
@File ：_xpath.py
@IDE ：PyCharm
@Motto:人生苦短，我学Python

"""
"""
封装XPath解析类
"""
from lxml import etree


class XPath:
    def __init__(self, html):
        self.html = html


class HtmlTree(XPath):
    def __init__(self, html: str):
        """
        网络爬取下来的HTML文档树解析操作类
        :param html: 正规HTML文档对象
        """
        super().__init__(html)

    def analysis(self, path: str) -> list:
        """
        解析
        :param path: 查询表达式 符合xpath语法
        :return: 结果列表
        """
        tree = etree.HTML(self.html)
        res_lis = tree.xpath(path)
        return res_lis

    def __str__(self):
        return f"{str(self.html)}"


class ParseTree(HtmlTree):
    def __init__(self, html: str):
        """
        本地的HTML文档树解析操作类
        :param html: 本地HTML文件路径
        """
        super().__init__(html)

    def analysis(self, path: str) -> list:
        tree = etree.parse(self.html)
        res_lis = tree.xpath(path)
        return res_lis


if __name__ == '__main__':
    html = HtmlTree('html')
    print(html)
    parse = ParseTree('../test.html').analysis("//ul/li")
    print(parse)
